package com.bignerdranch.travelcommunity.ui.dynamic

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.lifecycle.MutableLiveData
import com.bignerdranch.tclib.LogUtil.eeeee
import com.bignerdranch.travelcommunity.R

import com.bignerdranch.travelcommunity.base.BaseDialogFragment

import com.bignerdranch.travelcommunity.databinding.ProgressFragmentBinding
import kotlinx.android.synthetic.main.progress_fragment.*

class ProgressFragment:BaseDialogFragment<ProgressFragmentBinding>(),View.OnClickListener{
    override val layoutId: Int = R.layout.progress_fragment
    override val needLogin: Boolean = false
    override val themeResId: Int = R.style.Dialog_Bottom
    override var windowHeight: Double = 0.4
    var  progress1 =  MutableLiveData(0)
    var  progress2 =  MutableLiveData(0)
    var  progress3 =  MutableLiveData(0)
    var  progress4 =  MutableLiveData(0)

    interface  LoadButtonClickListener{
        fun onClicked(which:Int);
    }

    interface  PauseButtonClickListener{
        fun onClicked(which:Int);
    }

    var   loadButtonClickListener:LoadButtonClickListener? = null
    var  pauseButtonClickListener:PauseButtonClickListener? = null


    fun setLoadButtonClickListener(loadButtonClickListener: LoadButtonClickListener):ProgressFragment{
        this.loadButtonClickListener = loadButtonClickListener
        return this
    }


    fun setPauseButtonClickListener(pauseButtonClickListener: PauseButtonClickListener):ProgressFragment{
        this.pauseButtonClickListener = pauseButtonClickListener
        return this
    }


    fun updateProgress(which:Int,progress:Int){
        when(which){
            0-> progress1.postValue(progress)
            1-> progress2.postValue(progress)
            2-> progress3.postValue(progress)
            3-> progress4.postValue(progress)
            else -> throw  IndexOutOfBoundsException("$which is out of the bound range")
        }
    }

    override fun subscribeObserver() {
        super.subscribeObserver()
        progress1.observeForever{
             binding.progress1 = it
            if(it>=100){
                binding.load1.text = "完成"
                binding.pause1.isEnabled = false
                binding.pause1.setBackgroundResource(R.drawable.load_button_unable)
            }else if (it>0){
                binding.load1.text = "继续"
            }
        }

        progress2.observeForever{
            binding.progress2 = it
            if(it>=100){
                binding.load2.text = "完成"
                binding.pause2.isEnabled = false
                binding.pause2.setBackgroundResource(R.drawable.load_button_unable)
            }else if (it>0){
                binding.load2.text = "继续"
            }

        }

        progress3.observeForever{
            binding.progress3 = it
            if(it>=100){
                binding.load3.text = "完成"
                binding.pause3.isEnabled = false
                binding.pause3.setBackgroundResource(R.drawable.load_button_unable)
            }else if (it>0){
                binding.load3.text = "继续"
            }

        }

        progress4.observeForever{
            binding.progress4 = it
            if(it>=100) {
                binding.load4.text = "完成"
                binding.pause4.isEnabled = false
                binding.pause4.setBackgroundResource(R.drawable.load_button_unable)
            }else if (it>0){
                binding.load4.text = "继续"
            }
        }
    }

    override fun subscribeListener() {
        super.subscribeListener()
        with(binding){
            load1.setOnClickListener {
                 loadButtonClickListener?.onClicked(0)
            }

            load2.setOnClickListener {
                loadButtonClickListener?.onClicked(1)
            }

            load3.setOnClickListener {
                loadButtonClickListener?.onClicked(2)
            }

            load4.setOnClickListener {
                loadButtonClickListener?.onClicked(3)
            }


            pause1.setOnClickListener {
                 pauseButtonClickListener?.onClicked(0)
            }

            pause2.setOnClickListener {
                pauseButtonClickListener?.onClicked(1)
            }
            pause3.setOnClickListener {
                pauseButtonClickListener?.onClicked(2)
            }
            pause4.setOnClickListener {
                pauseButtonClickListener?.onClicked(3)
            }
        }
    }


    fun setListener(button:Button,block:()->Unit){
         button.setOnClickListener {
           if(pauseButtonClickListener!=null && loadButtonClickListener!=null)
               block()
         }
    }



    override fun onClick(p0: View?) {
/*
        eeeee("点击了 ")
        var which = -1
        var type = 0
        when(p0?.id){
            R.id.load_1 ->  which = 1
            R.id.load_2 -> which = 2
            R.id.load_3 -> which = 3
            R.id.load_4 -> which = 4
            R.id.pause_1 -> {
                which = 1
                type = 1
            }
            R.id.pause_2 -> {
                which = 2
                type = 1
            }
            R.id.pause_3 -> {
                which = 3
                type = 1
            }
            R.id.pause_4 -> {
                which = 4
                type = 1
            }
            else -> throw  UnknownError("异常点击事件，无法捕获到被点击的对象Id!")
        }



        if(type == 0)
        pauseButtonClickListener?.onClicked(which)
        else loadButtonClickListener?.onClicked(which)*/
    }



}